package sqlClass.appClass;

import general.appClass.SystemInfo;
import general.dbClass.DBCategory;
import general.dbClass.DBKeyword;
import general.dbClass.DBPicInfo;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sqlClass.daoClass.DaoUpdate;

/**
 * Servlet implementation class ImgDeleteApp
 */
@WebServlet("/ImgUpdateApp")
public class ImgUpdateApp extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImgUpdateApp() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html; charset=UTF-8");

		//System Information Manage Class
		Connection 	conn = SystemInfo.getConn(request);
		
		//Parameter "action"
		String action = request.getParameter("action");
		if (action != null && action.equals("deleteImage")) {
			this.deleteImage(request, response, conn);
			return;
		}
		if (action != null && action.equals("updateImage")) {
			this.updateImage(request, response, conn);
			return;
		}
		if (action != null && action.equals("updateImageTooltip")) {
			this.updateImageTooltip(request, response, conn);
			return;
		}

	}

	private void updateImage(HttpServletRequest request,
			HttpServletResponse response, Connection con)
			throws ServletException, IOException {
		int nr = Integer.parseInt(request.getParameter("bnr"));
		String name = request.getParameter("imgName");
		int rate = Integer.parseInt(request.getParameter("imgRate"));
		int lock = 1;
		ArrayList<DBCategory> categories = new ArrayList<DBCategory>();
		ArrayList<DBKeyword> keywords = new ArrayList<DBKeyword>();

		String[] selectedCategories = request.getParameterValues("category");
		String selectedKeywords = request.getParameter("keywords");
		if (selectedCategories!=null){
			for (int i = 0; i < selectedCategories.length; i++) {
				DBCategory tmp = new DBCategory(
						Integer.parseInt(selectedCategories[i]), null);
				categories.add(tmp);
			}	
		}
		

		String delims = ",";
		String[] keywordsInput = selectedKeywords.split(delims);

		for (int i = 0; i < keywordsInput.length; i++) {
			if (keywordsInput[i].equals(""))
				break;
			
			DBKeyword keyword = new DBKeyword(
					Integer.parseInt(keywordsInput[i]), null);
			keywords.add(keyword);
		}

		DBPicInfo picInfo = new DBPicInfo(nr, name, rate, categories,
				keywords);

		try {
			DaoUpdate.updatePicInfo(con, nr, lock, picInfo);

			request.setAttribute("msg", "Update Picture successfully!");
			String url = response.encodeURL("/successful.jsp");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();

			String message = e.getMessage();
			request.setAttribute("Quelle", "connect");
			request.setAttribute("Error", message);
			String url = response.encodeURL("/errorInfo.jsp");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		}
	}

	private void updateImageTooltip(HttpServletRequest request,
			HttpServletResponse response, Connection con)
			throws ServletException, IOException {
		int nr = Integer.parseInt(request.getParameter("bnr"));
		String name = request.getParameter("imgName");
		int rate = 0;
		try{
			rate = Integer.parseInt(request.getParameter("imgRate"));	
		}catch(Exception e){
			rate = 0;
		}
		
		int lock = 1;
		ArrayList<DBCategory> categories = new ArrayList<DBCategory>();
		ArrayList<DBKeyword> keywords = new ArrayList<DBKeyword>();

		String[] selectedCategories = request.getParameterValues("category");
		String[] selectedKeywords = request.getParameterValues("keyword");
		if (selectedCategories!=null){
			for (int i = 0; i < selectedCategories.length; i++) {
				DBCategory tmp = new DBCategory(
						Integer.parseInt(selectedCategories[i]), null);
				categories.add(tmp);
			}	
		}
		if (selectedKeywords!=null){
			for (int i = 0; i < selectedKeywords.length; i++) {
				DBKeyword tmp = new DBKeyword(
						Integer.parseInt(selectedKeywords[i]), null);
				keywords.add(tmp);
			}	
		}
		
		DBPicInfo picInfo = new DBPicInfo(nr, name, rate, categories,
				keywords);

		try {
			DaoUpdate.updatePicInfo(con, nr, lock, picInfo);

			String url = response.encodeURL("/"+SystemInfo.getMode(request.getSession())+"ImgInfoApp?action=tooltip&bnr="+nr);
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();

			String message = e.getMessage();
			request.setAttribute("Quelle", "connect");
			request.setAttribute("Error", message);
			String url = response.encodeURL("/errorInfo.jsp");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		}
	}

	
	private void deleteImage(HttpServletRequest request,
			HttpServletResponse response, Connection conn)
			throws ServletException, IOException {
		String[] bnrStringArray = request.getParameterValues("bnr");
		try {
			for (int i = 0; i < bnrStringArray.length; i++) {
				int id = Integer.parseInt(bnrStringArray[i]);
				DaoUpdate.deletePic(conn, id);
			}
			//System.out.println("delete fertig!");
			request.setAttribute("msg", "delete Image successfully!");
			String url = response.encodeURL("/successful.jsp");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
			String message = e.getMessage();
			request.setAttribute("Quelle", "connect");
			request.setAttribute("Error", message);
			String url = response.encodeURL("/errorInfo.jsp");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		}


	}

}
